package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caocaog.market.entity.Serve;
import com.caocaog.market.service.ServeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.Trim;
import org.jeecg.common.system.base.controller.GenericController;
import org.jeecg.common.system.domain.QueryRequest;
import org.jeecg.common.system.query.PageGenerator;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 服务信息
 *
 * @author chechen
 * @date 2021-11-09
 */
@Slf4j
@RestController
@Api(value = "serve", tags = ServeController.TABLE_MEANING)
@RequestMapping("/serve")
public class ServeController extends GenericController<Serve, ServeService> {

    public static final String TABLE_MEANING = "服务信息";

    private static final String PAGE = "服务信息-分页列表查询";
    @Trim
    @ApiOperation(value = PAGE, notes = PAGE)
    @GetMapping(value = "/list")
    @RequiresPermissions("serve:view")
    public Result<?> queryPageList(Serve serve, QueryRequest queryRequest) {
        Page<Serve> page = PageGenerator.createPageSort(queryRequest);
        IPage<Serve> pageList = super.service.queryServePage(page, serve);
        return Result.OK(pageList);
    }

    private static final String ADD = "服务信息-添加";
    @AutoLog(value = ADD)
    @ApiOperation(value = ADD, notes = ADD)
    @PostMapping(value = "/add")
    @RequiresPermissions("serve:add")
    public Result<?> add(@RequestBody Serve serve) {
        return Result.OK(ADD_SUCCESS, super.service.addServe(serve));
    }

    private static final String EDIT = "服务信息-编辑";
    @AutoLog(value = EDIT)
    @ApiOperation(value = EDIT, notes = EDIT)
    @PutMapping(value = "/edit")
    @RequiresPermissions("serve:edit")
    public Result<?> edit(@RequestBody Serve serve) {
        return Result.OK(EDIT_SUCCESS, super.service.updateServe(serve));
    }

    private static final String PUT_ON_SALE = "服务信息-上架";
    @AutoLog(value = PUT_ON_SALE)
    @ApiOperation(value = PUT_ON_SALE, notes = PUT_ON_SALE)
    @PutMapping(value = "/putOnSale")
    @RequiresPermissions("serve:upDown")
    public Result<?> putOnSaleServe(@RequestParam("ids") String ids) {
        return Result.OK("上架服务信息成功", super.service.putOnSaleServe(ids));
    }

    private static final String PULL_OFF_SHELVES = "服务信息-下架";
    @AutoLog(value = PULL_OFF_SHELVES)
    @ApiOperation(value = PULL_OFF_SHELVES, notes = PULL_OFF_SHELVES)
    @PutMapping(value = "/pullOffShelves")
    @RequiresPermissions("serve:upDown")
    public Result<?> pullOffShelvesServe(@RequestParam("ids") String ids) {
        return Result.OK("下架服务信息成功", super.service.pullOffShelvesServe(ids));
    }

    private static final String DELETE = "服务信息-通过id删除";
    @AutoLog(value = DELETE)
    @ApiOperation(value = DELETE, notes = DELETE)
    @DeleteMapping(value = "/delete")
    @RequiresPermissions("serve:delete")
    public Result<?> delete(@RequestParam("id") String id) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteServe(id));
    }

    private static final String DELETE_BATCH = "服务信息-批量删除";
    @AutoLog(value = DELETE_BATCH)
    @ApiOperation(value = DELETE_BATCH, notes = DELETE_BATCH)
    @DeleteMapping(value = "/deleteBatch")
    @RequiresPermissions("serve:delete")
    public Result<?> deleteBatch(@RequestParam("ids") String ids) {
        return Result.OK(DELETE_SUCCESS, super.service.deleteBatchServe(Arrays.asList(ids.split(","))));
    }

}
